package com.ypbase.cloud.tags.api.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.BaseDubboException;
import com.alibaba.dubbo.rpc.BaseDubboServerException;
import com.cloud.framework.core.common.dto.PageResultDto;
import com.cloud.framework.core.dal.repo.mybatis.PageResult;
import com.cloud.framework.core.log.LoggerFactory;
import com.ypbase.cloud.tags.api.ITagApi;
import com.ypbase.cloud.tags.api.dto.TagCategoryDto;
import com.ypbase.cloud.tags.api.dto.TagDto;
import com.ypbase.cloud.tags.api.impl.assembler.TagAssembler;
import com.ypbase.cloud.tags.common.dictionary.TagExceptionEnum;
import com.ypbase.cloud.tags.entity.Tag;
import com.ypbase.cloud.tags.entity.TagCategory;
import com.ypbase.cloud.tags.service.ITagCategoryService;
import com.ypbase.cloud.tags.service.ITagService;

public class TagApiImpl implements ITagApi {

	private static final Logger logger = LoggerFactory.getLogger(TagApiImpl.class);

	@Autowired
	private ITagService tagService;

	@Autowired
	private ITagCategoryService tagCategoryService;

	/**
	 * 分页条件查询
	 */
	@Override
	public PageResultDto<TagDto> getTagPageList(TagDto tagDto, Long pageNo, Long pageSize)
			throws BaseDubboServerException, BaseDubboException {
		PageResult<Tag> pageResult = new PageResult<Tag>();
		pageResult.setPageNo(pageNo);
		pageResult.setPageSize(pageSize);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (tagDto.getCompanyId() == null) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "companyId"));
		} else {
			paramMap.put("companyId", tagDto.getCompanyId());
		}
		if (tagDto.getCategoryId() != null) {
			paramMap.put("categoryId", tagDto.getCategoryId());
		}
		if (!StringUtils.isBlank(tagDto.getCategoryCname())) {
			paramMap.put("categoryCname", tagDto.getCategoryCname());
		}
		if (!StringUtils.isBlank(tagDto.getEname())) {
			paramMap.put("ename", tagDto.getEname());
		}
		if (!StringUtils.isBlank(tagDto.getCname())) {
			paramMap.put("cname", tagDto.getCname());
		}
		if (tagDto.getStatus() != null) {
			paramMap.put("status", tagDto.getStatus());
		}

		try {
			pageResult = tagService.getTagPageList(pageResult, paramMap);

		} catch (Throwable e) {
			e.printStackTrace();
			logger.error(e.toString());
			throw new BaseDubboServerException(TagExceptionEnum.SYSTEM_ERROR.getCode(),
					TagExceptionEnum.SYSTEM_ERROR.getMsg());
		}

		return TagAssembler.toDto(pageResult);
	}

	/***
	 * 添加标签
	 */
	@Override
	public void addTag(TagDto tagDto) throws BaseDubboServerException, BaseDubboException {

		if (tagDto.getCompanyId() == null) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "companyId"));
		}
		if (StringUtils.isBlank(tagDto.getCname())) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "cnName"));
		}
		if (tagDto.getType() == null) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "type"));
		}

		// 找到是否存在该标签
		Tag tag = new Tag();
		tag.setCompanyId(tagDto.getCompanyId());
		tag.setCname(tagDto.getCname());
		List<Tag> listTag = tagService.findSampleViaBatis(tag);
		if (listTag.size() > 0) {
			throw new BaseDubboException(TagExceptionEnum.USER_EXISTS.getCode(),
					String.format(TagExceptionEnum.USER_EXISTS.getMsg()));
		}

		try {
			// 判断标签分类是否存在
			TagCategory tagCategory = new TagCategory();
			tagCategory.setId(tagDto.getCategoryId());
			List<TagCategoryDto> tagCategoryDtos = tagCategoryService.geTagCategoryList(tagCategory);
			if (tagCategoryDtos.size() > 0) {
				// 添加标签
				tagService.addTag(tagDto);
			} else {
				throw new BaseDubboException(TagExceptionEnum.PARAM_ILLEGAL.getCode(),
						String.format(TagExceptionEnum.PARAM_ILLEGAL.getMsg(), "categoryId"));
			}
		} catch (Throwable e) {
			e.printStackTrace();
			logger.error(e.toString());
			throw new BaseDubboServerException(TagExceptionEnum.SYSTEM_ERROR.getCode(),
					TagExceptionEnum.SYSTEM_ERROR.getMsg());
		}

	}

	/***
	 * 修改标签
	 */
	@Override
	public void updateTag(TagDto tagDto) throws BaseDubboServerException, BaseDubboException {

		if (null == tagDto.getId()) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "id"));
		}

		try {

			// 找到是否存在该标签
			Tag tag = tagService.getViaBatis(tagDto.getId());
			if (tag == null) {
				throw new BaseDubboException(TagExceptionEnum.USER_NOT_EXISTS.getCode(),
						String.format(TagExceptionEnum.USER_NOT_EXISTS.getMsg()));
			}
			tag.setCompanyId(tagDto.getCompanyId());
			tag.setCategoryId(tagDto.getCategoryId());
			tag.setCategoryCname(tagDto.getCategoryCname());
			tag.setCname(tagDto.getCname());
			tag.setDescript(tagDto.getDescript());
			tag.setType(tagDto.getType());
			tagService.updateTag(tag);

		} catch (Throwable e) {
			e.printStackTrace();
			logger.error(e.toString());
			throw new BaseDubboServerException(TagExceptionEnum.SYSTEM_ERROR.getCode(),
					TagExceptionEnum.SYSTEM_ERROR.getMsg());
		}
	}

	/***
	 * 删除标签
	 */
	@Override
	public void deleteTagById(Long id) throws BaseDubboServerException, BaseDubboException {

		if (null == id) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "id"));
		}
		try {
			// 找到是否存在该标签
			Tag tag = tagService.getViaBatis(id);
			if (tag == null) {
				throw new BaseDubboException(TagExceptionEnum.USER_NOT_EXISTS.getCode(),
						String.format(TagExceptionEnum.USER_NOT_EXISTS.getMsg()));
			}
			tagService.deleteTagById(tag);

		} catch (Throwable e) {
			e.printStackTrace();
			logger.error(e.toString());
			throw new BaseDubboServerException(TagExceptionEnum.SYSTEM_ERROR.getCode(),
					TagExceptionEnum.SYSTEM_ERROR.getMsg());
		}
	}

}
